<?php
error_reporting(E_ALL ^ E_NOTICE);

use Net\Connection;
use Net\Event;

require_once './Net/Connection.php';
require_once './Net/Event.php';

$client = new Connection();

$client
    ->onReady(function() use($client) {
        echo "连接成功\n";
        $client->send('hello hello hello');
        sleep(1);
        $client->send('are you ok');
        sleep(1);
    
        $message = $client->recv();
        echo "服务器回复: $message\n";
    
        sleep(3);
        $client->send('exit');
    })
    
    ->onRecv(function(Event $e) {
        echo "服务端回复: {$e->getMessage()}\n";
    })
    
    ->onConnectFail(function(Event $e) use($client) {
        echo "连接失败: {$e->getErrorCode()} {$e->getErrorMessage()}\n";
        die;
    })
    
    ->onClose(function() {
        die;
    })
;

$client->connect('127.0.0.1', 30001);

Connection::dispatch();
